package com.dwusoft.ius;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.apache.api.api.ApiService;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SpringContextLoader;
import com.apache.database.constant.SystemTools;
import com.apache.tools.DataMap;
import com.dwusoft.ius.plugin.CustomMethodPlugin;

public class CustomMethodFactory {

	private static CustomMethodFactory instance;
	//private String filePath = "";
	//private long lastUpateTime = 0l;//最后修改时间
	private Map<String, CustomMethodPlugin> plugins = new HashMap<String, CustomMethodPlugin>();

	private CustomMethodFactory() {
		//filePath = Validator.getClassLoaderPath() + "config/";
	}

	public synchronized static CustomMethodFactory getInstance() {
		if (null == instance) {
			instance = new CustomMethodFactory();
		}
		return instance;
	}

	/**
	  * description: 加载配置文件
	 * @throws Exception 
	 */
	private CustomMethodPlugin initConfig(String key) {
		//File oldfile = new File(filePath + "custom-method.properties");
		//long lastModletime = oldfile.lastModified();
		//		if (lastUpateTime < lastModletime) {//有更新从新加载
		//		if (true) {
		try {
			if (plugins.isEmpty()) {
				//plugins = new HashMap<String, CustomMethodPlugin>();
				ApiService netProxyManager = (ApiService) SpringContextLoader.getBean("iusService");
				ParamsVo params = new ParamsVo();
				//params.setParams("doCode", "dymicSql");
				params.setParams("sqlKeyId", "TrpPluginManager");
				params.setParams("resultType", "list");
				params.setMethodKey("dymicSql");
				params.setParams("sysPass", SystemTools.getInstance().getValue("push_to_dwusoft_pass"));
				ResultEntity dymicSqlParamResult = netProxyManager.doService(params);
				List<DataMap> list = (List) dymicSqlParamResult.getEntity();
				int count = list.size();
				for (int i = 0; i < count; i++) {
					DataMap dmap = list.get(i);
					String pluginKey = String.valueOf(dmap.get("pluginFullName"));
					System.out.println("正在加载:" + pluginKey);
					try {
						Class<?> clazz = Class.forName(pluginKey);
						CustomMethodPlugin plugin = (CustomMethodPlugin) clazz.newInstance();
						plugins.put(String.valueOf(dmap.get("pluginShortName")), plugin);
						//setCacheInfo(String.valueOf(dmap.get("pluginShortName")), plugin);
					} catch (Exception e) {
						System.err.println("加载插件失败，请检查[" + pluginKey + "]配置或者插件是否存在");
					}
				}
			}
			//}
		} catch (Exception e) {
			System.out.println("注入接口失败！");
			return null;
		}
		if (Validator.isNull(key)) {
			return null;
		} else {
			return plugins.get(key);
		}
	}

	@SuppressWarnings("unchecked")
	private void setCacheInfo(String key, CustomMethodPlugin plugin) {
		Map<String, CustomMethodPlugin> obj = (Map) SystemTools.getInstance().getCache("")
				.getCacheObjectByKey("custom_plugins");
		if (null == obj || obj.isEmpty()) {
			obj = new HashMap<String, CustomMethodPlugin>();
		}
		obj.put(key, plugin);
		SystemTools.getInstance().getCache("").createCacheObject("custom_plugins", obj);
	}

	public void setPlugins(String key, String value) {
		try {
			if (Validator.isNull(key) || Validator.isNull(value))
				return;
			Class<?> clazz = Class.forName(value.trim());
			plugins.put(key.trim(), (CustomMethodPlugin) clazz.newInstance());
		} catch (Exception e) {
			System.out.println("加载:" + value + "失败");
		}
	}

	public CustomMethodPlugin getPluginByKey(String key) {
		return initConfig(key);
	}
}
